﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxClasses.Internal;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;

namespace Besture.UI
{
    public partial class HotelsSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //绑定地区位置
            var dt = new DataTable();
            dt = new HotelsCategoryDac().GetDataTableByCategoryType(Constants.HOTEL_CATEGORY_LOCATION.ToString());
            ControlHelper.BindDropDownList(ddlArea, dt, "CategoryName", "ID");

            //绑定酒店星级
            dt = new HotelsCategoryDac().GetDataTableByCategoryType(Constants.HOTEL_CATEGORY_STARS.ToString());
            ControlHelper.BindDropDownList(ddlHotelsStars, dt, "CategoryName", "ID");

            //绑定酒店特色
            dt = new HotelsCategoryDac().GetDataTableByCategoryType(Constants.HOTEL_CATEGORY_CHARACTERISTIC.ToString());
            ControlHelper.BindDropDownList(ddlCharacteristicID, dt, "CategoryName", "ID");

            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
            AspxGridViewHelper.InitAspxGridView(grid);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            InitPage();
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            var dac = new TrafficDac();
            grid.DataSource = dac.GetDataTableByHotelsId(ID);
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridCustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new HotelsDac();
            var model = dac.GetModel(id);
            if (model == null) return;

            txtHotelsName.Text = model.HotelsName.ToString();
            imgHotelsImage.ImageUrl = model.HotelsImage.ToString();
            txtTel.Text = model.Tel.ToString();
            txtAddress.Text = model.Address.ToString();
            /*txtLongitude.Text = model.Longitude.ToString();
            txtLatitude.Text = model.Latitude.ToString();*/
            txtDetail.Text = String.IsNullOrEmpty(model.Detail) ? "" : model.Detail.ToString();
            txtNetworkFacilities.Text = String.IsNullOrEmpty(model.NetworkFacilities)?"":model.NetworkFacilities.ToString();
            txtParkingLot.Text = model.ParkingLot;
            txtHotelFacilities.Text = model.HotelFacilities;
            ddlArea.SelectedValue = model.Area.ToString() == "" ? "" : model.Area.ToString();
            ddlHotelsStars.SelectedValue = model.HotelsStars.ToString() == "" ? "" : model.HotelsStars.ToString();
            ddlCharacteristicID.SelectedValue = model.CharacteristicID.ToString() == "" ? "" : model.CharacteristicID.ToString();
            selectBrand.selectClassID = model.BrandID.ToString() == "" ? "" : model.BrandID.ToString();
            //ddlFacilitiesID.SelectedValue = model.FacilitiesID.ToString() == "" ? "" : model.FacilitiesID.ToString();
            ddlDate.Text = model.LatestTime;
            ddlFastTime.Text = model.FastTime;
            txtIntroduction.Text = model.Introduction;
            imgHotelsImage1.ImageUrl = model.WidthImageUrl;
            cbRecommendationList.Checked = model.RecommendationList == Constants.RECOMMENDATION_YES ? true : false;
            cbRecommendationPay.Checked = model.RecommendationPay == Constants.RECOMMENDATION_YES ? true : false;
            cbRecommendationHome.Checked = model.RecommendationHome == Constants.RECOMMENDATION_YES ? true : false;
            cbTopHotels.Checked = model.TopHotels == Constants.RECOMMENDATION_YES ? true : false;
            txtSortID.SortValue = model.SortID != 0 ? model.SortID : 0;

            ControlSeo.SeoTitle = model.SeoTitle;
            ControlSeo.SeoKeywords = model.SeoKeywords;
            ControlSeo.SeoDescription = model.SeoDescription;

            //绑定多图
            UploadMultiPhoto.InitPhotoTable = new ImageDac().GetDataTableByImageType(model.ID, Constants.IMAGE_TYPE_HOTELS);

            //绑定设施
            var dt = new HotelsFacilitiesDac().GetDataTableByHotelsId(model.ID);
            var ids = "";
            var names = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ids += dt.Rows[i]["FacilitiesID"]+",";
                names += dt.Rows[i]["CategoryName"] + ",";
            }
            btnFacility.Value = names == "" ? "请选择设施" : names;
            txtFacilityValues.Text = ids == "" ? "" : ids;
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            //多图
            UploadMultiPhotoModel uploadMultiPhotoModel = new UploadMultiPhotoModel();
            //删除图片集合
            uploadMultiPhotoModel.DelIdArgs = StringHelper.SplitString(UploadMultiPhoto.DelIdArgs.TrimEnd(','));
            //添加图片集合
            uploadMultiPhotoModel.AddImgUrlArgs = StringHelper.SplitString(UploadMultiPhoto.AddImgUrlArgs);
            uploadMultiPhotoModel.AddImgRemarParamIDkArgs = StringHelper.SplitString(UploadMultiPhoto.AddImgRemarParamIDkArgs);
            //更新图片集合
            uploadMultiPhotoModel.ImgIdArgs = StringHelper.SplitString(UploadMultiPhoto.ImgIdArgs.TrimEnd(','));
            uploadMultiPhotoModel.ImgUrlArgs = StringHelper.SplitString(UploadMultiPhoto.ImgUrlArgs.TrimEnd(','));
            uploadMultiPhotoModel.ImgRemarParamIDkArgs = StringHelper.SplitString(UploadMultiPhoto.ImgRemarkArgs);
            uploadMultiPhotoModel.ImgId = StringHelper.SplitString(UploadMultiPhoto.ImgId);

            var dac = new HotelsDac();
            HotelsModel model = null;
            var baseDac = new BaseDac<HotelsModel>();
            //添加
            if (!EditPage)
            {
                model = new HotelsModel();
                model.HotelsName = txtHotelsName.Text.Trim();
                model.HotelsImage = imgHotelsImage.ImageUrl.ToString();
                model.Tel = txtTel.Text.Trim();
                model.Address = txtAddress.Text.Trim();
                /*model.Longitude = txtLongitude.Text.Trim();
                model.Latitude = txtLatitude.Text.Trim();*/
                model.Detail = txtDetail.Text;
                model.NetworkFacilities = txtNetworkFacilities.Text;
                model.ParkingLot = txtParkingLot.Text;
                model.HotelFacilities = txtHotelFacilities.Text;
                model.Area = ddlArea.SelectedValue == "" ? 0 : Convert.ToInt32(ddlArea.SelectedValue);
                model.HotelsStars = ddlHotelsStars.SelectedValue == "" ? 0 : Convert.ToInt32(ddlHotelsStars.SelectedValue);
                model.CharacteristicID = ddlCharacteristicID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlCharacteristicID.SelectedValue);
                model.BrandID = Convert.ToInt32(selectBrand.selectClassID);
                //model.FacilitiesID = ddlFacilitiesID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlFacilitiesID.SelectedValue);
                model.LatestTime = ddlDate.SelectedValue;
                model.FastTime =ddlFastTime.Text;
                model.Introduction = txtIntroduction.Text.Trim();
                model.WidthImageUrl = imgHotelsImage1.ImageUrl;
                model.RecommendationList = cbRecommendationList.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.RecommendationPay = cbRecommendationPay.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.RecommendationHome = cbRecommendationHome.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.TopHotels = cbTopHotels.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;

                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
                //加载SEO基本数据
                baseDac.LoadSeo(model, ControlSeo.SeoTitle, ControlSeo.SeoKeywords, ControlSeo.SeoDescription);

                dac.Add(model, uploadMultiPhotoModel, txtFacilityValues.Text.Trim());
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.HotelsName = txtHotelsName.Text.Trim();
                model.HotelsImage = imgHotelsImage.ImageUrl.ToString();
                model.Tel = txtTel.Text.Trim();
                model.Address = txtAddress.Text.Trim();
                /*model.Longitude = txtLongitude.Text.Trim();
                model.Latitude = txtLatitude.Text.Trim();*/
                model.Detail = txtDetail.Text;
                model.NetworkFacilities = txtNetworkFacilities.Text;
                model.ParkingLot = txtParkingLot.Text;
                model.HotelFacilities = txtHotelFacilities.Text;
                model.Area = ddlArea.SelectedValue == "" ? 0 : Convert.ToInt32(ddlArea.SelectedValue);
                model.HotelsStars = ddlHotelsStars.SelectedValue == "" ? 0 : Convert.ToInt32(ddlHotelsStars.SelectedValue);
                model.CharacteristicID = ddlCharacteristicID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlCharacteristicID.SelectedValue);
                model.BrandID = Convert.ToInt32(selectBrand.selectClassID);
                //model.FacilitiesID = ddlFacilitiesID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlFacilitiesID.SelectedValue);
                model.LatestTime = ddlDate.SelectedValue;
                model.FastTime = ddlFastTime.Text;
                model.Introduction = txtIntroduction.Text.Trim();
                model.WidthImageUrl = imgHotelsImage1.ImageUrl;
                model.RecommendationList = cbRecommendationList.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.RecommendationPay = cbRecommendationPay.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.RecommendationHome = cbRecommendationHome.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.TopHotels = cbTopHotels.Checked == true ? Constants.RECOMMENDATION_YES : Constants.RECOMMENDATION_NO;
                model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;

                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);
                //加载SEO基本数据
                baseDac.LoadSeo(model, ControlSeo.SeoTitle, ControlSeo.SeoKeywords, ControlSeo.SeoDescription);

                dac.Update(model, uploadMultiPhotoModel, txtFacilityValues.Text.Trim());
            }

            FunHelper.Alert("保存成功", ListUrl());
        }
        #endregion

        #region 取消
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        protected void BtnCancelClick(object sender, EventArgs eventArgs)
        {
           //删除酒店ID为0的酒店地图信息
            TrafficDac trafficDac = new TrafficDac();
            trafficDac.DeleteByHotelsId(0);

            Response.Redirect("/admin/Hotels/HotelsList.aspx");
        }
        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridRowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            var id = Convert.ToInt32(e.Keys[0]);
            var dac = new TrafficDac();
            dac.Delete(id);
            InitPage();
        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDelClick(object sender, EventArgs e)
        {
            var ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            var dac = new TrafficDac();
            dac.DeleteList(ids);
            FunHelper.Alert("删除成功！", Request.Url.ToString());
        }

        #endregion
    }
}